import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

import java.sql.*;

public class DatabaseInstance {
    //预设值，可通过配置文件覆盖
    private String host = "localhost";
    private int port = 3306;
    private String database = "db_minecraft";
    private String username = "root";
    private String password = "root";

    private static DatabaseInstance instance = null;
    private Connection connection = null;
    private Statement statement = null;

    public static DatabaseInstance getInstance() {
        //当数据库实例为空，初始化并返回实例，否则直接返回实例
        return instance == null ? instance = new DatabaseInstance() : instance;
    }

    public void enable() throws SQLException {
        //获取当前执行的插件主体
        JavaPlugin plugin = JavaPlugin.getPlugin(Main.class);
        //从插件主体中获取配置信息
        FileConfiguration config = plugin.getConfig();
        //从配置信息中获取数据库配置，有则覆盖，无则跳过
        if (config.contains("mysql")) {
            if (config.contains("mysql.host"))
                host = config.getString("mysql.host");
            if (config.contains("mysql.port"))
                port = config.getInt("mysql.port");
            if (config.contains("mysql.database"))
                database = config.getString("mysql.database");
            if (config.contains("mysql.username"))
                username = config.getString("mysql.username");
            if (config.contains("mysql.password"))
                password = config.getString("mysql.password");
        }
        //建立连接
        connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true", username, password);
        //建立执行器
        statement = connection.createStatement();
    }

    public void disable() {
        try {
            //关闭连接
            connection.close();
        } catch (SQLException e) {
        }
    }

    public Location getBack(Player player) {
        try {
            String name = player.getName();
            ResultSet rs = statement.executeQuery("SELECT * FROM `player_back` WHERE `player` = \"" + name + "\"");
            if (rs.next()) {
                Location location = player.getLocation();
                location.setWorld(Bukkit.getWorld(rs.getString("world")));
                location.setX(rs.getDouble("location_x"));
                location.setY(rs.getDouble("location_y"));
                location.setZ(rs.getDouble("location_z"));
                return location;
            } else {
                return null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void delBack(Player player) {
        try {
            String name = player.getName();
            statement.execute("DELETE FROM `player_back` WHERE player = \"" + name + "\"");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void setBack(Player player) {
        try {
            String name = player.getName();
            Location location = player.getLocation();
            String world = location.getWorld().getName();
            Double locationX = location.getX();
            Double locationY = location.getY();
            Double locationZ = location.getZ();
            ResultSet rs = statement.executeQuery("SELECT 1 FROM `player_back` WHERE `player` = \"" + name + "\"");
            if (rs.next()) {
                String sql = "UPDATE `player_back` SET `world` = \"" + world + "\",`location_x` = " + locationX + ",`location_y`=" + locationY + ",`location_z`=" + locationZ + " WHERE `player` = \"" + name + "\"";
                statement.execute(sql);
            } else {
                String sql = "INSERT INTO `player_back` (`player`, `world`,`location_x`,`location_y`,`location_z`) VALUES ( \"" + name + "\", \"" + world + "\", " + locationX + "," + locationY + "," + locationZ + ")";
                statement.execute(sql);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public Location getHome(Player player) {
        try {
            String name = player.getName();
            ResultSet rs = statement.executeQuery("SELECT * FROM `player_home` WHERE `player` = \"" + name + "\"");
            if (rs.next()) {
                Location location = player.getLocation();
                location.setWorld(Bukkit.getWorld(rs.getString("world")));
                location.setX(rs.getDouble("location_x"));
                location.setY(rs.getDouble("location_y"));
                location.setZ(rs.getDouble("location_z"));
                return location;
            } else {
                return null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void delHome(Player player) {
        try {
            String name = player.getName();
            statement.execute("DELETE FROM `player_home` WHERE player = \"" + name + "\"");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void setHome(Player player) {
        try {
            String name = player.getName();
            Location location = player.getLocation();
            String world = location.getWorld().getName();
            Double locationX = location.getX();
            Double locationY = location.getY();
            Double locationZ = location.getZ();
            ResultSet rs = statement.executeQuery("SELECT 1 FROM `player_home` WHERE `player` = \"" + name + "\"");
            if (rs.next()) {
                String sql = "UPDATE `player_home` SET `world` = \"" + world + "\",`location_x` = " + locationX + ",`location_y`=" + locationY + ",`location_z`=" + locationZ + " WHERE `player` = \"" + name + "\"";
                statement.execute(sql);
            } else {
                String sql = "INSERT INTO `player_home` (`player`, `world`,`location_x`,`location_y`,`location_z`) VALUES ( \"" + name + "\", \"" + world + "\", " + locationX + "," + locationY + "," + locationZ + ")";
                statement.execute(sql);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
